﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class ItemCtrl : MonoBehaviour
{
    public static List<Vector3> touches;
    public static float nearD = 2;
    private Vector3 lastPos;

    void Start()
    {
        lastPos = transform.position;
    }
    public virtual void Check(float force)
    {
        float minD = 999;
        Vector3 touch = Vector3.zero;
        for (int i = 0; i < touches.Count; i++)
        {
            float d = Vector3.Distance(transform.position, touches[i]);
            if (d < minD)
            {
                minD = d;
                touch = touches[i];
            }
        }
        if (minD < 2)
        {
            Vector3 dir = (transform.position - touch).normalized;
            float an = Vector3.Angle(dir, Vector3.up);
            an = an > 90 ? an - 90 : an;
            //float d = Mathf.Lerp(3f, 50f, an / 90) * (1 - minD / 2);
            float d = 25f * (1 - minD / 2);
            Do(transform.position + dir * d);
        }
    }

    public void Do(Vector3 endPos)
    {
        transform.DOMove(endPos, 0.3f).OnComplete(() =>
        {
            transform.DOMove(lastPos, 0.4f, true).SetDelay(3f);
        });
        transform.DORotate(new Vector3(0, 0, transform.rotation.eulerAngles.z + Random.Range(-90f, 90f)), 0.4f).OnComplete(() =>
        {
            transform.DORotate(new Vector3(0, 0, transform.rotation.eulerAngles.z + Random.Range(-90f, 90f)), 0.4f).SetDelay(3f);
        });
    }
}
